package com.flyduck.system.service;

import com.flyduck.common.exception.BusinessException;
import com.flyduck.common.util.RSAUtil;
import com.flyduck.common.util.TokenUtil;
import com.flyduck.system.dao.SysUserDao;
import com.flyduck.system.entity.SysUserEntity;
import com.flyduck.system.exception.SystemExceptionCode;
import com.flyduck.system.vo.userCenter.trans.SysUserCenterUpdateReqVOToSysUserEntity;
import com.flyduck.system.vo.user.trans.SysUserEntityToSysPersonDetailRespVO;
import com.flyduck.system.vo.userCenter.SysUserCenterDetailRespVO;
import com.flyduck.system.vo.userCenter.SysUserCenterUpdateAvatarReqVO;
import com.flyduck.system.vo.userCenter.SysUserCenterUpdatePasswordReqVO;
import com.flyduck.system.vo.userCenter.SysUserCenterUpdateReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @program: flyduck-platform
 * @description:
 * @author: flyduck
 * @create: 2021-03-17 22:02
 **/
@Service
public class SysUserCenterService {

    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private SysUserDao sysUserDao;

    /**
     * 获取个人详细信息
     * @return
     */
    public SysUserCenterDetailRespVO getUserCenterDetail() {
        String userName = tokenUtil.getLoginInfo().getUserInfo().getUserName();
        SysUserEntity userEntity = sysUserDao.getUserByUserName(userName);
        SysUserCenterDetailRespVO sysUserCenterDetailRespVO = new SysUserEntityToSysPersonDetailRespVO().apply(userEntity);
        return sysUserCenterDetailRespVO;
    }

    /**
     * 修改个人信息
     * @param reqVO
     * @return
     */
    public boolean updatePerson(SysUserCenterUpdateReqVO reqVO) {
        SysUserEntity userEntity = new SysUserCenterUpdateReqVOToSysUserEntity().apply(reqVO);
        userEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getUserName());
        userEntity.setUpdateTime(new Date());
        return sysUserDao.updateById(userEntity);
    }

    /**
     * 修改个人密码
     * @param reqVO
     * @return
     */
    public boolean updatePassword(SysUserCenterUpdatePasswordReqVO reqVO) {
        String userName = tokenUtil.getLoginInfo().getUserInfo().getUserName();
        SysUserEntity userEntity = sysUserDao.getUserByUserName(userName);
        if(!reqVO.getNewPassword().equals(reqVO.getCheckPassword())){
            throw new BusinessException(SystemExceptionCode.PASSWORD_NOT_MATCH);
        }
        if(!RSAUtil.check(reqVO.getOldPassword(),userEntity.getPassword())){
            throw new BusinessException(SystemExceptionCode.OLD_PASSWORD_NOT_MATCH);
        }
        userEntity.setPassword(RSAUtil.encrypt(reqVO.getNewPassword()));
        userEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getUserName());
        userEntity.setUpdateTime(new Date());
        return sysUserDao.updateById(userEntity);
    }

    /**
     * 修改头像
     * @param reqVO
     * @return
     */
    public boolean updateAvatar(SysUserCenterUpdateAvatarReqVO reqVO) {
        String userName = tokenUtil.getLoginInfo().getUserInfo().getUserName();
        SysUserEntity userEntity = sysUserDao.getUserByUserName(userName);
        userEntity.setAvatar(reqVO.getAvatar());
        userEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getUserName());
        userEntity.setUpdateTime(new Date());
        return sysUserDao.updateById(userEntity);

    }
}
